package com.lexed.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.lexed.database.Database;
import com.lexed.pojo.Emp;
import com.lexed.util.Util;

public class EmpService {
	
	public List<Emp> selectAll() throws Exception{
		
		ResultSet resultSet = 
		Database.getStatement("select * from emp").executeQuery();
		
		List<Emp> emps =  Util.getEmps(resultSet);
		
		return emps;
	}
	
	public Emp selectByUname(String uname) throws SQLException, Exception{
		ResultSet resultSet = 
				Database.getStatement("select * from emp where uname=?",uname).executeQuery();
		
		Emp emp = Util.getEmp(resultSet);
		return emp;
	}
	
	public Emp selectById(int id) throws SQLException, Exception{
		ResultSet resultSet = 
		Database.getStatement("select * from emp where id=?",id).executeQuery();
		
		Emp emp =  Util.getEmp(resultSet);
		
		return emp;
	}
	
	public void deleteById(int id) throws Exception{
		Database.getStatement("delete from emp where id=?",id).executeUpdate();
		
	}
	
	public void update(Emp emp) throws SQLException, Exception{
		Database.getStatement("update emp set uname=?,pwd=?,sal=?,dept=?,prm=? where id=?",emp.getUname(),emp.getPwd(),emp.getSal(),emp.getDept(),emp.getPrm(),emp.getId()).executeUpdate();
	}
	
	public void insert(Emp emp) throws Exception{
		Database.getStatement("insert into emp(uname,pwd,sal,dept,prm) values(?,?,?,?,?)",emp.getUname(),emp.getPwd(),emp.getSal(),emp.getDept(),emp.getPrm()).executeUpdate();
		
	}
	

}
